/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.classify;

import phoside.data.feature.model.Instance;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author gjj
 */
public class SemiLearnerFromPositiveAndUnlabeled implements SemiLearner, Serializable {
    private static final long serialVersionUID = 821199208258956721L;
    
    protected final List<Instance> positive;
    protected final List<Instance> unlabeled;
    protected final BinaryClassifier classifier;
    protected List<Double> result;

    public SemiLearnerFromPositiveAndUnlabeled(final List<Instance> positive,
                                               final List<Instance> unlabeled,
                                               final BinaryClassifier classifier) {
        if (positive==null || unlabeled==null) {
            throw new NullPointerException();
        }

        this.positive = positive;
        this.unlabeled = unlabeled;
        this.classifier = classifier;
    }

    public boolean runOneEpoch() {
        List<Instance> instances = new ArrayList(unlabeled);
        instances.addAll(positive);

        List<Double> label;
        if (result==null) {
            label = new ArrayList(getDefaultLabel(unlabeled.size(), -1.0));
            label.addAll(getDefaultLabel(positive.size(), 1.0));
        } else {
            label = new ArrayList(result);
            double sum = 0;
            for (double r : result) {
                if (r>0) {
                    sum += r;
                }
            }

            int npos = positive.size();
            label.addAll(getDefaultLabel(npos, sum*5.0/npos));
        }

        System.out.println("Training...");
        if (classifier.train(instances, label)) {
            System.out.println("Classifying...");
            result = classifier.classify(unlabeled);
            return true;
        } else {
            return false;
        }
    }

    public List<Double> test(final List<Instance> instances) {
        if (instances==null) {
            throw new NullPointerException();
        }

        return classifier.classify(instances);
    }

    public List<Double> getResult() {
        if (result==null) {
            throw new IllegalStateException("Run at least one epoch.");
        }
        
        return result;
    }

    protected List<Double> getDefaultLabel(final int n, double value) {
        List<Double> label = new ArrayList(n);

        for (int i=0; i<n; i++) {
            label.add(value);
        }

        return label;
    }

}
